﻿<%@ WebHandler Language="C#" Class="xinxi_gongyingshang" %>


using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_gongyingshang : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["gongYingShangType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_gongyingshang");
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string gongyingshangmingcheng = string.Format("{0}", context.Request.Form["gongyingshangmingcheng"]);
                        string gongyingshangzhuangtai = string.Format("{0}", context.Request.Form["gongyingshangzhuangtai"]);
                        string gongyingshangid = string.Format("{0}", context.Request.Form["gongyingshangid"]);
                        if (string.IsNullOrEmpty(gongyingshangid))
                        {
                            string createCondition = m_DB.CreateAndCondition("mingcheng", DataValueCommonType.字符串, gongyingshangmingcheng, "", SearchConditionType.等于);
                            DataTable danWeiDt = m_DB.GetDataTableByCondition("tbl_gongyingshang", createCondition);
                            if (danWeiDt.Rows.Count > 0)
                            {
                                context.Response.Write("已存在同名的供应商信息，请检查。");
                                return;
                            }
                            //新增供应商信息
                            TableRow newRow = new TableRow("tbl_gongyingshang", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol();
                            col1.ColName = "mingcheng";
                            col1.ValueMember = gongyingshangmingcheng;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "zhuangtai";
                            col2.ValueMember = gongyingshangzhuangtai;
                            col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "bianhao";
                            col3.ValueMember = "";
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "shangchuanzhuangtai";
                            col4.ValueMember = "1";
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "zhandian";
                            col5.ValueMember = "云端";
                            col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "kehubianhao";
                            col6.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);

                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }

                        }
                        else
                        {
                            //修改供应商
                            string createCondition = m_DB.CreateAndCondition("mingcheng", DataValueCommonType.字符串, gongyingshangmingcheng, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("id", DataValueCommonType.字符串, gongyingshangid, "", SearchConditionType.不等于);
                            DataTable danWeiDt = m_DB.GetDataTableByCondition("tbl_gongyingshang", createCondition);
                            if (danWeiDt.Rows.Count > 0)
                            {
                                context.Response.Write("已存在同名的供应商信息，请检查。");
                                return;
                            }
                            TableRow editRow = new TableRow("tbl_gongyingshang", gongyingshangid, DBCtl.修改);
                            TableCol col1 = new TableCol();
                            col1.ColName = "mingcheng";
                            col1.ValueMember = gongyingshangmingcheng;
                            col1.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "bianhao";
                            col2.ValueMember = "";
                            col2.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "zhuangtai";
                            col3.ValueMember = gongyingshangzhuangtai;
                            col3.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col3);

                            if (m_DB.SaveTableRows(editRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        break;
                    case "3":
                        //删除供应商信息
                        string delGongYingShangID = string.Format("{0}", context.Request.Form["gongyingshangid"]);
                        if (!string.IsNullOrEmpty(delGongYingShangID))
                        {
                            TableRow delRow = new TableRow();
                            delRow.ID = delGongYingShangID;
                            delRow.TableName = "tbl_gongyingshang";
                            if (m_DB.DeleteRowByID(delRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            context.Response.Write("获取信息失败，请重试。");
                        }
                        break;
                    case "4":
                        string searchCondition = m_DB.CreateAndCondition("zhuangtai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
                        DataTable returnDt = m_DB.GetDataTableByCondition("tbl_gongyingshang",searchCondition);
                        string responseJson = JsonConvert.SerializeObject(returnDt);
                        context.Response.Write(responseJson);
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("供应商信息异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}